package com.example.yuejian.yuejian.model;

import com.example.yuejian.yuejian.base.ModleListListener;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.SignupTrain;
import com.example.yuejian.yuejian.bean.Train;
import com.example.yuejian.yuejian.bean.TrainInfo;
import com.example.yuejian.yuejian.service.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/9.
 */

public class TrainModel {
    private Retrofit retrofit;
    public TrainModel(){
        retrofit=new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Contants.BASE_URL)
                .build();
    }
    public void TrainAllList(final ModleListListener listener)
    {
        TrainService service=retrofit.create(TrainService.class);
        Call<List<Train>>call=service.trainlist();
        call.enqueue(new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void Traintypelist(int type,final ModleListListener listListener)
    {
        TrainService service=retrofit.create(TrainService.class);
        Call<List<Train>>call=service.traintypelist(type);
         call.enqueue(new Callback<List<Train>>() {
             @Override
             public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                 if (response.isSuccessful() && response.body() != null) {
                     listListener.onResponse(response.body());
                 } else
                     listListener.onFail("onresponse fail");
             }

             @Override
             public void onFailure(Call<List<Train>> call, Throwable t) {
                 listListener.onFail(t.toString());
             }
         });
    }
    public void Traindetail(String id, final ModleListener listener)
    {
        TrainService service=retrofit.create(TrainService.class);
        Call<TrainInfo>call=service.traindetail(id);
        call.enqueue(new Callback<TrainInfo>() {
            @Override
            public void onResponse(Call<TrainInfo> call, Response<TrainInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<TrainInfo> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    //活动报名
    public  void signupTrain(String trainid,String userid,int num,String username,
                                String  phone,double money,String  pay_type,final ModleListener listener)
    {
       TrainService service=retrofit.create(TrainService.class);
        Call<SignupTrain>call=service.signuptrain(trainid,userid,num,username,phone,money,pay_type);
        call.enqueue(new Callback<SignupTrain>() {
            @Override
            public void onResponse(Call<SignupTrain> call, Response<SignupTrain> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SignupTrain> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
